<template>
	<view class="mainpadding">
		<u-swiper :list="list1" circular height="170px" @click="lbtclick"></u-swiper>
		<view class="margin_top">
			<u-notice-bar :text="text1" direction="column" @click="enternews($event)"></u-notice-bar>
		</view>
		<u-tabs :list="tabslist" :scrollable="false" lineColor="#3478f5" @click="changetab" :activeStyle="{
		    color: '#3478f5',
		    fontWeight: 'bold',
		    transform: 'scale(1.05)'
		}" :inactiveStyle="{
		            color: '#333333',
					fontWeight: '540',
		            transform: 'scale(1)'
		        }" itemStyle="padding-left: 10px; padding-right: 10px; height: 44px;"></u-tabs>
		<template v-if="tabsid==1">
			<view class="mainpadding radius margin_top ffffff" v-for="item in cc_list" :key="item.id"
				@click="feitian(item)">
				<view class="hongbox">
					<view class="xiaobai sanshier nofonweight textcenter">{{item.name}}
						{{item.start_time}}-{{item.end_time}}
					</view>
				</view>
				<view class="flexleft margin_top2">
					<view class="line margin_right1"></view>
					<view class="xiaohei bigtext xiaohei fonweight">{{item.name}}</view>
				</view>
				<view class="banner margin_top2">
					<image :src="item.image_text" mode=""></image>
				</view>
			</view>
		</template>
		<template v-if="tabsid==2">
			<view class="mainpadding radius margin_top ffffff" @click="jumper('/pages/homepage/duihuanlieb','')">
				<view class="flexleft">
					<view class="line margin_right1"></view>
					<view class="xiaohei bigtext xiaohei fonweight">兑换区</view>
				</view>
				<view class="banner margin_top2">
					<image src="../../static/image/sysem/dhq.png" mode=""></image>
				</view>
			</view>
		</template>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				text1: [],
				list1: [],
				newsData: [],
				bannerList: [],
				cc_list: [],
				tabslist: [{
					name: "抢购区",
					id: 1,
				}, {
					name: "商城",
					id: 2,
				}],
				tabsid: 1,
				userInfo: {},
				paraString:"",
			}
		},
		onLoad() {
			this.paraString = window.location.href //还可以  window.location.search.substring()
			let text = this.getQueryVariable('invite_code') //code是url后面带的
			if(text){
				uni.setStorageSync("invite_code",text.substring(0,text.length-2))
			}
			this.getbanner()
			this.getnews()
			this.getfeitian()
			
		},
		onShow() {
			if (httpRequest.checkIsLogin()) {
				this.userinfoinit()
			}
		},
		methods: {
			getQueryVariable(variable) {
				var query = this.paraString;
				var rooms = query.split('?');
				for (var i = 0; i < rooms.length; i++) {
					var pair = rooms[i].split('=');
					if (pair[0] == variable) {
						return (pair[1].split('&'))[0]
					}
				}
				//或者拼接&后面的值
				var vars = query.split('&')
				for (var i = 0; i < vars.length; i++) {
					var pair = vars[i].split('=')
					if (pair[0] == variable) {
						return pair[1]
					}
				}
			},
			userinfoinit() {
				httpRequest.request('/api/user/index', 'GET', {}).then(res => {
					this.userInfo = res.data
				})
			},
			feitian(item) {
				if (!httpRequest.checkIsLogin()) {
					uni.navigateTo({
						url: "/pages/login/login"
					})
					return false
				}
				if(this.userInfo.is_cjqg==1){
					this.jumper('/pages/homepage/feitianchang?id=', item.id)
					return false
				} 
				// if(this.userInfo.is_qg==0){
				// 	httpRequest.toast("暂无资格进入")
				// 	return false
				// }
				let yuanshijian2 = this.haomiao2(item.start_time)
				// let yuanshijian2 = this.haomiao2("11:15")
				console.log(yuanshijian2,"测试测试");
				let text2 = this.checkAuditTime(yuanshijian2, item.end_time)
				// let text2 = this.checkAuditTime(yuanshijian2, "16:00")
				if(text2){
					this.jumper('/pages/homepage/feitianchang?id=', item.id)
				}else{
					let yuanshijian = this.haomiao(item.start_time)
					// let yuanshijian = this.haomiao("11:15")
					let text = this.checkAuditTime(yuanshijian, item.end_time)
					// let text = this.checkAuditTime(yuanshijian, "16:00")
					// let text = this.checkAuditTime("14:00","16:00")
					if (text) {
						this.jumper('/pages/homepage/feitianchang?id=', item.id)
					} else {
						httpRequest.toast("未到开场时间")
					}
					// httpRequest.toast("未到开场时间")
				}
				
			},
			checkAuditTime(beginTime, endTime) {
				var nowDate = new Date();
				var beginDate = new Date(nowDate);
				var endDate = new Date(nowDate);
			
				var beginIndex = beginTime.lastIndexOf("\:");
				var beginHour = beginTime.substring(0, beginIndex);
				var beginMinue = beginTime.substring(beginIndex + 1, beginTime.length);
				beginDate.setHours(beginHour, beginMinue, 0, 0);
			
				var endIndex = endTime.lastIndexOf("\:");
				var endHour = endTime.substring(0, endIndex);
				var endMinue = endTime.substring(endIndex + 1, endTime.length);
				endDate.setHours(endHour, endMinue, 0, 0);
				return nowDate.getTime() - beginDate.getTime() >= 0 && nowDate.getTime() <= endDate.getTime();
			},
			haomiao(start_time) {
				let text1 = (start_time[0] == 0 ? '' : start_time[0]) + start_time[1]
				let text2 = (start_time[3] == 0 ? '' : start_time[3]) + start_time[4]
				console.log(text1, "888");
				let num = (Number(text1) * 60 * 60 * 1000 + Number(text2) * 60 * 1000) - Number(this.userInfo
					.tq_time_text) * 1000
				console.log(this.msToTime(num), "9999");
				return this.msToTime(num)
			},
			haomiao2(start_time) {
				let text1 = (start_time[0] == 0 ? '' : start_time[0]) + start_time[1]
				let text2 = (start_time[3] == 0 ? '' : start_time[3]) + start_time[4]
				console.log(text1, "888");
				let num = (Number(text1) * 60 * 60 * 1000 + Number(text2) * 60 * 1000) - Number(this.userInfo
					.yy_tq_time_text) * 1000
				console.log(this.msToTime(num), "999921");
				return this.msToTime(num)
			},
			msToTime(ms) {
				const seconds = Math.floor((ms / 1000) % 60);
				const minutes = Math.floor((ms / (1000 * 60)) % 60);
				const hours = Math.floor((ms / (1000 * 60 * 60)) % 24);
				return (hours < 10 ? '0' + hours : hours) + ':' + (minutes < 10 ? '0' + minutes : minutes)
			},
			
			changetab(e) {
				console.log(e, 999)
				this.tabsid = e.id
			},
			getfeitian() {
				httpRequest.request("/api/cccl/ccIndex", 'GET', {}, false, false, true).then(res => {
					this.cc_list = res.data
				})
			},
			getbanner() {
				httpRequest.request("/api/index/getAdvertiseList", 'POST', {
					advertise_position_id: 4,
					is_more: 1
				}, false, false, true).then(res => {
					res.data.forEach(item => {
						this.list1.push(item.image_text)
					})
					this.bannerList = res.data
				})
			},
			lbtclick(index) {
				if (this.bannerList[index].url) {
					uni.navigateTo({
						url: "/pages/system/webview?url=" + this.bannerList[index].url,
						fail(err) {
							console.log(err, "惊雷");
						}
					})
				}
			},
			getnews() { //滚动消息
				httpRequest.request('/api/index/niticeindex', 'GET', {
					page: 1,
					limit: 12
				}, false, false, true).then(res => {
					let arr = []
					res.data.data.forEach(item => {
						arr.push(item.title)
					})
					this.text1 = arr
					this.newsData = res.data.data
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			enternews(index) {
				uni.navigateTo({
					url: "/pages/mypage/gonggaoxq?id=" + this.newsData[index].id
				})
			},
			jumper(url, val) {
				uni.navigateTo({
					url: url + val
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.hongbox {
		border-radius: 20rpx;
		padding: 10rpx 30rpx;
		background-color: #f5423d;
	}

	.line {
		width: 9rpx;
		height: 33rpx;
		border-radius: 10rpx;
		background-color: #3478f5;
	}

	.banner {
		width: 100%;
		height: 220rpx;
		border-radius: 20rpx;

		image {
			border-radius: 20rpx;
			width: 100%;
			height: 100%;
		}
	}
</style>